﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Transactions;

public partial class Main_TaxMaster : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strSQL = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            _BindTaxType();
            _BindAdditionalTaxDetails();
            if (Request.QueryString["status"] != null)
            {
                if (Request.QueryString["status"].ToString() == "Edit" && Request.QueryString["TxDetId"] != null)
                {
                    _BindDataForEdit(Request.QueryString["TxDetId"].ToString());
                    hidTaxDetId.Value = Request.QueryString["TxDetId"].ToString();
                    hidTaxMasterId.Value = Request.QueryString["TaxMasterId"].ToString();
                    _BindShortName(ddlTaxType.SelectedValue, ddlMainClass.SelectedValue);
                    _BindAllTaxDetails(ddlTaxType.SelectedValue, ddlMainClass.SelectedValue, ddlSubClass.SelectedValue);
                }
            }
            else
            {                
                _BindAllTaxDetails();
                hyperTDS.Disabled = true; 
            }                       
        }
    }
    private void _BindTaxType()
    {
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@TAXCATEGORY","1")
                                };
        DataTable dttaxtype = DU.getdatausingprocedure("USP_ACC_GetTaxTypeBasedOnTaxCategory",sparam);
        ddlTaxType.DataSource = dttaxtype;
        ddlTaxType.DataTextField = "TAXTYPE";
        ddlTaxType.DataValueField = "TAXTYPEID";
        ddlTaxType.DataBind();
        ddlTaxType.Items.Insert(0, new ListItem("-Select-", "0"));
    }
    private void _BindMainClass(string strTaxTypeId)
    {
        ddlMainClass.Items.Clear();
        ddlSubClass.Enabled = false;
        if (strTaxTypeId != string.Empty && strTaxTypeId != "0")
        {
            strSQL = "SELECT MAINCLASSID,MAINCLASSNAME FROM ACC_TAXMAINCLASS WHERE TAXTYPEID='" + strTaxTypeId + "' ";            
            DataTable DTMainClass = DU.getdata(strSQL);
            if (DTMainClass.Rows.Count > 0)
            {
                if (DTMainClass.Rows.Count == 0)
                {                 
                    ddlMainClass.Items.Insert(0, new ListItem("", DTMainClass.Rows[0]["MAINCLASSID"].ToString()));                    
                    ddlMainClass.Enabled = false;                   
                    _BindSubClass(strTaxTypeId, ddlMainClass.SelectedValue);
                }
                else if (DTMainClass.Rows.Count>=1)
                {
                    ddlMainClass.Enabled = true;
                    ddlMainClass.DataSource = DTMainClass;
                    ddlMainClass.DataTextField = "MAINCLASSNAME";
                    ddlMainClass.DataValueField = "MAINCLASSID";
                    ddlMainClass.DataBind();
                    ddlMainClass.Items.Insert(0, new ListItem("-Select-", "0"));
                }
                
                
            }
            else
            {
                ddlMainClass.Items.Insert(0, new ListItem("", "0"));
                ddlMainClass.Enabled = false;                
            }
        }
        else
        {
            ddlMainClass.Items.Insert(0, new ListItem("", "0"));
            ddlMainClass.Enabled = false;
            
        }
    }
    private void _BindSubClass(string strTaxTypeId, string strMainClassId)
    {
        ddlSubClass.Items.Clear();      
        strSQL = "SELECT SUBCLASSID,SUBCLASSNAME FROM UVW_ACC_TaxMaster WHERE TAXTYPEID='" + strTaxTypeId + "' AND MAINCLASSID='" + strMainClassId + "'";
        DataTable DTSubClass = DU.getdata(strSQL);
        if (DTSubClass.Rows.Count > 0)
        {
            if (DTSubClass.Rows.Count == 1)
            {
                ddlSubClass.Items.Insert(0, new ListItem("", DTSubClass.Rows[0]["SUBCLASSID"].ToString()));
                ddlSubClass.Enabled = false;                
            }
            else if (DTSubClass.Rows.Count > 1)
            {

                ddlSubClass.Enabled = true;
                ddlSubClass.DataSource = DTSubClass;
                ddlSubClass.DataTextField = "SUBCLASSNAME";
                ddlSubClass.DataValueField = "SUBCLASSID";
                ddlSubClass.DataBind();
                ddlSubClass.Items.Insert(0, new ListItem("-Select-", "0"));
            }
        }
        else
        {
            ddlSubClass.Items.Insert(0, new ListItem("", "0"));
            ddlSubClass.Enabled = false;
        }        
    }
    private void _BindShortName(string strTaxTypeId, string strMainClassId)
    {
        ddlShortName.Items.Clear();
        if (strTaxTypeId != string.Empty)
        {
            DataTable dtsubclass = DU.getdata("SELECT TAXMASTERID,SHORTNAME FROM UVW_ACC_TaxMaster WHERE TAXTYPEID='" + strTaxTypeId + "' AND MAINCLASSID='" + strMainClassId + "'");
            ddlShortName.DataSource = dtsubclass;
            ddlShortName.DataTextField = "SHORTNAME";
            ddlShortName.DataValueField = "TAXMASTERID";
            ddlShortName.DataBind();
        }
        
    }
    private void _BindShortName(string strTaxTypeId)
    {
        ddlShortName.Items.Clear();
        if (strTaxTypeId != string.Empty)
        {
            SqlParameter [] sparam=
                                    {
                                        new SqlParameter("@TAXTYPEID",strTaxTypeId)
                                    };
            DataTable DTShortName = DU.getdatausingprocedure("USP_ACC_GetTaxShortName", sparam);
            ddlShortName.DataSource = DTShortName;
            ddlShortName.DataTextField = "SHORTNAME";
            ddlShortName.DataValueField = "TAXMASTERID";
            ddlShortName.DataBind();
        }       
    }
    private void _BindTaxLedger(string strTaxTypeId)
    {
        ddlTaxLedger.Items.Clear();
        if (strTaxTypeId != string.Empty)
        {
            SqlParameter [] sparam=
                                    {
                                        new SqlParameter("@TAXTYPEID",strTaxTypeId)
                                    };
            DataTable DTShortName = DU.getdatausingprocedure("USP_ACC_GetTaxLedgers", sparam);
            ddlTaxLedger.DataSource = DTShortName;
            ddlTaxLedger.DataTextField = "LNAME";
            ddlTaxLedger.DataValueField = "LID";
            ddlTaxLedger.DataBind();
        }       
    }
    private void _BindLedger(string strTaxTypeId,string strMainClassId)
    {
        ddlTaxLedger.Items.Clear();
        string sql = "SELECT SUBCLASSID,SUBCLASSNAME FROM UVW_ACC_TaxMaster WHERE TAXTYPEID='" + strTaxTypeId + "' AND MAINCLASSID='"+strMainClassId+"'";
        DataTable dt = DU.getdata(sql);
        if (dt.Rows.Count > 0)
        {
            ddlTaxLedger.DataSource = dt;
            ddlTaxLedger.DataValueField = "SUBCLASSID";
            ddlTaxLedger.DataTextField = "SUBCLASSNAME";
            ddlTaxLedger.DataBind();
            ddlTaxLedger.Items.Insert(0, new ListItem("- Select Ledger -", "0"));
        }
    }
    private void _BindAdditionalTaxDetails()
    {
        DataTable dtadd = DU.getdatausingprocedure("USP_ACC_GetAdditionalTaxes");
        grdAdditionalTax.DataSource = dtadd;
        grdAdditionalTax.DataBind();
    }    
    private void _BindAdditionalTaxLedger(string strTaxmasterId, DropDownList ddlAddTaxLedger)
    {
        if (strTaxmasterId != string.Empty)
        {
            SqlParameter[] sparam =
                                    {
                                        new  SqlParameter ("@TAXMASTERID",strTaxmasterId)
                                    };
            DataTable DTAddTaxLedger = DU.getdatausingprocedure("USP_ACC_GetAdditionalTaxLedger", sparam);
            if (DTAddTaxLedger.Rows.Count > 0)
            {
                ddlAddTaxLedger.DataSource = DTAddTaxLedger;
                ddlAddTaxLedger.DataTextField = "LNAME";
                ddlAddTaxLedger.DataValueField = "LID";
                ddlAddTaxLedger.DataBind();
                ddlAddTaxLedger.Items.Insert(0, new ListItem("", "0"));
            }
            else
            {
                ddlAddTaxLedger.Items.Insert(0, new ListItem("", "0"));
            }
        }        
    }
    private void _EnableDisableTDSServiceTaxTab(string strTaxTypeId)
    {
        if (strTaxTypeId != string.Empty)
        {
            if (strTaxTypeId == "3" || strTaxTypeId == "8" || strTaxTypeId == "12" || strTaxTypeId == "13")
            {
                hyperTDS.Disabled = false;
                if (strTaxTypeId == "13")
                {
                    panelTDSThresholdLimit.Enabled = true;
                    txtTDSPerBill.Text = "0";
                    txtPerAnum.Text = "0";
                }
                else
                {
                    panelTDSThresholdLimit.Enabled = false;
                    txtTDSPerBill.Text = "0";
                    txtPerAnum.Text = "0";
                }

            }
            else
            {
                hyperTDS.Disabled = true;
                panelTDSThresholdLimit.Enabled = false;
                txtTDSPerBill.Text = "0";
                txtPerAnum.Text = "0";
            }
        }
        else
        {
           
            hyperTDS.Disabled = true;
            panelTDSThresholdLimit.Enabled = false;
            txtTDSPerBill.Text = "0";
            txtPerAnum.Text = "0";
        }
    }
    private void _GetDefaultTaxRate(string strMainClassId, string strSubClassId)
    {
        if (strMainClassId != string.Empty && strSubClassId != string.Empty)
        {
            strSQL = "SELECT DEFTAXRATE FROM UVW_ACC_TaxMaster WHERE MAINCLASSID='" + strMainClassId + "' AND SUBCLASSID='" + strSubClassId + "' ";
            DataTable DTDefTaxRate = DU.getdata(strSQL);
            if (DTDefTaxRate.Rows.Count > 0)
            {
                string strDefaultTaxRate = DTDefTaxRate.Rows[0]["DEFTAXRATE"].ToString();
                if (strDefaultTaxRate == string.Empty)
                {
                    strDefaultTaxRate = "0";
                }
                txtTaxRate.Text = string.Format("{0:0.00}", Convert.ToDouble(strDefaultTaxRate));
            }
        }
    }     
    private void _BindAllTaxDetails()
    {
        strSQL = "SELECT * FROM UVW_ACC_TaxDetails ORDER BY TAXTYPEID ASC";
        DataTable DTTaxDetails = DU.getdata(strSQL);
        grdTaxDetails.DataSource = DTTaxDetails;
        grdTaxDetails.DataBind();
    }
    private void _BindAllTaxDetails(string strTaxTypeId)
    {
        strSQL = "SELECT * FROM UVW_ACC_TaxDetails WHERE TAXTYPEID='" + strTaxTypeId + "' ORDER BY TAXTYPEID ASC";
        DataTable DTTaxDetails = DU.getdata(strSQL);
        grdTaxDetails.DataSource = DTTaxDetails;
        grdTaxDetails.DataBind();
    }
    private void _BindAllTaxDetails(string strTaxTypeId, string strMainClassId)
    {
        strSQL = "SELECT * FROM UVW_ACC_TaxDetails WHERE TAXTYPEID='" + strTaxTypeId + "' AND MAINCLASSID='" + strMainClassId + "'";
        DataTable DTTaxDetails = DU.getdata(strSQL);
        grdTaxDetails.DataSource = DTTaxDetails;
        grdTaxDetails.DataBind();
    }
    private void _BindAllTaxDetails(string strTaxTypeId, string strMainClassId, string strSubClassId)
    {
        strSQL = "SELECT * FROM UVW_ACC_TaxDetails WHERE TAXTYPEID='" + strTaxTypeId + "' AND MAINCLASSID='" + strMainClassId + "' AND SUBCLASSID='" + strSubClassId + "'";
        DataTable DTTaxDetails = DU.getdata(strSQL);
        grdTaxDetails.DataSource = DTTaxDetails;
        grdTaxDetails.DataBind();
    }
    private void _SaveTaxDetails(string strTaxDetID, string strTaxMasterId)
    {
        try
        {
            if (strTaxDetID != string.Empty && strTaxMasterId != string.Empty)
            {

                if (strTaxMasterId == "0")
                {
                    strSQL = "SELECT TAXMASTERID FROM UVW_ACC_TaxMaster WHERE TAXTYPEID='" + ddlTaxType.SelectedValue + "' AND MAINCLASSID='" + ddlMainClass.SelectedValue + "' AND SUBCLASSID='" + ddlSubClass.SelectedValue + "'";
                    DataTable DTTaxMaster = DU.getdata(strSQL);
                    if (DTTaxMaster.Rows.Count > 0)
                    {
                        strTaxMasterId = DTTaxMaster.Rows[0]["TAXMASTERID"].ToString();
                    }
                }
                if (strTaxMasterId != string.Empty)
                {
                    string effectiveFromDate = string.Empty;
                    string effectiveToDate = "06/06/2079";
                    if (txtFromDate.Text != string.Empty)
                    {
                        string[] str = txtFromDate.Text.Split('/');
                        effectiveFromDate = str[1] + "/" + str[0] + "/" + str[2];
                    }
                    if (txtToDate.Text != string.Empty && txtToDate.Text != "Till Date")
                    {
                        string[] str = txtToDate.Text.Split('/');
                        effectiveToDate = str[1] + "/" + str[0] + "/" + str[2];
                    }
                    if (txtTaxRate.Text == string.Empty)
                    {
                        txtTaxRate.Text = "0";
                    }

                    if (ddlTaxType.SelectedValue == "3" || ddlTaxType.SelectedValue == "8" || ddlTaxType.SelectedValue == "12" || ddlTaxType.SelectedValue == "13")
                    {
                        if (txtTDSPerBill.Text == string.Empty)
                        {
                            txtTDSPerBill.Text = "0";
                        }
                        if (txtPerAnum.Text == string.Empty)
                        {
                            txtPerAnum.Text = "0";
                        }
                        SqlParameter[] sparam ={
                                               new SqlParameter("@TXDETID",strTaxDetID),
                                               new SqlParameter("@TAXMASTERID",strTaxMasterId),
                                               new SqlParameter("@EFFFROMDATE",effectiveFromDate),
                                               new SqlParameter("@EFFTODATE",effectiveToDate),
                                               new SqlParameter("@TAXACTIVE","1"),
                                               new SqlParameter("@TAXRATE",txtTaxRate.Text.Trim()),
                                               new SqlParameter("@TAXRATE_LID",ddlTaxLedger.SelectedValue),
                                               new SqlParameter("@THRESHOLD",txtTDSPerBill.Text),
                                               new SqlParameter("@THRESHOLDFORYEAR",txtPerAnum.Text)
                                          };
                        int Result = DU.executeprocedure("USP_ACC_SaveTaxDetails", sparam);
                        if (Result > 0)
                        {
                            if (strTaxDetID == "0")
                            {
                                strSQL = "SELECT IDENT_CURRENT('ACC_TAXDETAILS') AS TAXDETID";
                                DataTable DTTaxDetId = DU.getdata(strSQL);
                                if (DTTaxDetId.Rows.Count > 0)
                                {
                                    _SaveAdditionalTax(DTTaxDetId.Rows[0]["TAXDETID"].ToString());
                                }
                            }
                            else
                            {
                                _SaveAdditionalTax(strTaxDetID);
                            }
                        }
                    }
                    else
                    {
                        SqlParameter[] sparam ={
                                               new SqlParameter("@TXDETID",strTaxDetID),
                                               new SqlParameter("@TAXMASTERID",strTaxMasterId),
                                               new SqlParameter("@EFFFROMDATE",effectiveFromDate),
                                               new SqlParameter("@EFFTODATE",effectiveToDate),
                                               new SqlParameter("@TAXACTIVE","1"),
                                               new SqlParameter("@TAXRATE",txtTaxRate.Text.Trim()),
                                               new SqlParameter("@TAXRATE_LID",ddlTaxLedger.SelectedValue)                                              
                                          };
                        int Result = DU.executeprocedure("USP_ACC_SaveTaxDetails", sparam);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }  
    private void _SaveAdditionalTax(string strTaxDetID)
    {
        try
        {
            if (strTaxDetID != string.Empty)
            {
                SqlParameter[] sparam ={
                                       new SqlParameter("@MAIN_TXDETID",strTaxDetID)
                                   };
                DU.executeprocedure("USP_IVR_DeleteAdditionalTaxes", sparam);
                for (int i = 0; i < grdAdditionalTax.Rows.Count; i++)
                {
                    HiddenField hidAddTaxmasterId = (HiddenField)grdAdditionalTax.Rows[i].FindControl("hidAddTaxmasterId");
                    CheckBox chkSelect = (CheckBox)grdAdditionalTax.Rows[i].FindControl("chkSelect");
                    TextBox txtAdditionalTaxRate = (TextBox)grdAdditionalTax.Rows[i].FindControl("txtAdditionalTaxRate");
                    DropDownList ddlAddTaxLedger = (DropDownList)grdAdditionalTax.Rows[i].FindControl("ddlAddTaxLedger");
                    if (chkSelect.Checked == true)
                    {
                        SqlParameter[] sparam1 ={
                                                new SqlParameter("@MAIN_TXDETID",strTaxDetID),
                                                new SqlParameter("@ADDITIONAL_TAXMASTERID",hidAddTaxmasterId.Value),
                                                new SqlParameter("@TAXRATE",txtAdditionalTaxRate.Text),
                                                new SqlParameter("@TAXRATELEDGERID",ddlAddTaxLedger.SelectedValue)
                                            };
                        DU.executeprocedure("USP_ACC_SaveAdditionalTaxApplicable", sparam1);
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private void _BindDataForEdit(string strTaxDetID)
    {
        if (strTaxDetID != string.Empty)
        {
            strSQL = "SELECT * FROM UVW_ACC_TaxDetails WHERE TXDETID='"+strTaxDetID +"'";
            DataTable DTBind = DU.getdata(strSQL);
            if (DTBind.Rows.Count > 0)
            {
                ddlTaxType.SelectedValue = DTBind.Rows[0]["TAXTYPEID"].ToString();
                _BindMainClass(DTBind.Rows[0]["TAXTYPEID"].ToString());
                _EnableDisableTDSServiceTaxTab(DTBind.Rows[0]["TAXTYPEID"].ToString());
                ddlMainClass.SelectedValue = DTBind.Rows[0]["MAINCLASSID"].ToString();
                _BindSubClass(DTBind.Rows[0]["TAXTYPEID"].ToString(), DTBind.Rows[0]["MAINCLASSID"].ToString());
                ddlSubClass.SelectedValue = DTBind.Rows[0]["SUBCLASSID"].ToString();
                txtFromDate.Text = string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(DTBind.Rows[0]["EFFFROMDATE"].ToString())).Replace("01/01/1900", "");
                txtToDate.Text = string.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(DTBind.Rows[0]["EFFTODATE"].ToString())).Replace("06/06/2079", "Till Date");
                string strTaxRate = DTBind.Rows[0]["TAXRATE"].ToString();
                if (strTaxRate == string.Empty)
                {
                    strTaxRate = "0";
                }               
                txtTaxRate.Text = string.Format("{0:0.00}", Convert.ToDouble(strTaxRate));
                _BindTaxLedger(DTBind.Rows[0]["TAXTYPEID"].ToString());
                ddlTaxLedger.SelectedValue = DTBind.Rows[0]["TAXRATE_LID"].ToString();
                txtTDSPerBill.Text = DTBind.Rows[0]["THRESHOLD"].ToString();
                txtPerAnum.Text = DTBind.Rows[0]["THRESHOLDFORYEAR"].ToString();
            }
            _BindAdditionalTaxForEdit(strTaxDetID);
        }
    }
    private void _BindAdditionalTaxForEdit(string strTaxDetID)
    {
        if (strTaxDetID != string.Empty)
        {           
            strSQL = "SELECT * FROM ACC_TAXADDITIONAL WHERE MAIN_TXDETID='" + strTaxDetID + "'";
            DataTable DTAddTax = DU.getdata(strSQL);
            if (DTAddTax.Rows.Count > 0)
            {
                for (int j = 0; j < DTAddTax.Rows.Count; j++)
                {
                    for (int i = 0; i < grdAdditionalTax.Rows.Count; i++)
                    {
                        HiddenField hidAddTaxmasterId = (HiddenField)grdAdditionalTax.Rows[i].FindControl("hidAddTaxmasterId");
                        CheckBox chkSelect = (CheckBox)grdAdditionalTax.Rows[i].FindControl("chkSelect");
                        TextBox txtAdditionalTaxRate = (TextBox)grdAdditionalTax.Rows[i].FindControl("txtAdditionalTaxRate");
                        DropDownList ddlAddTaxLedger = (DropDownList)grdAdditionalTax.Rows[i].FindControl("ddlAddTaxLedger");
                        if (hidAddTaxmasterId.Value == DTAddTax.Rows[j]["ADDITIONAL_TAXMASTERID"].ToString())
                        {
                            chkSelect.Checked = true;
                            string strTaxRate = DTAddTax.Rows[j]["TAXRATE"].ToString();
                            if (strTaxRate == string.Empty)
                            {
                                strTaxRate = "0";
                            }
                            txtAdditionalTaxRate.Text = string.Format("{0:0.00}", Convert.ToDouble(strTaxRate));
                            ddlAddTaxLedger.SelectedValue = DTAddTax.Rows[j]["TAXRATE_LID"].ToString();
                        }
                    }
                }
            }
        }
    }
    protected void ddlTaxType_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlSubClass.Items.Clear();
        ddlShortName.Items.Clear();
        _BindMainClass(ddlTaxType.SelectedValue);
        //_BindTaxLedger(ddlTaxType.SelectedValue);

        _EnableDisableTDSServiceTaxTab(ddlTaxType.SelectedValue);
        if (ddlMainClass.Items.Count == 1)
        {
            ddlMainClass.Enabled = false;
            ddlSubClass.Enabled = false;
            _BindShortName(ddlTaxType.SelectedValue);
        }
        _BindAllTaxDetails(ddlTaxType.SelectedValue);
        hidTaxDetId.Value = "0";
        hidTaxMasterId.Value = "0";
        _BindAdditionalTaxDetails();
    }
    protected void ddlMainClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        _BindAllTaxDetails(ddlTaxType.SelectedValue,ddlMainClass.SelectedValue);
        _BindSubClass(ddlTaxType.SelectedValue, ddlMainClass.SelectedValue);
        _BindLedger(ddlTaxType.SelectedValue, ddlMainClass.SelectedValue);
        _BindShortName(ddlTaxType.SelectedValue, ddlMainClass.SelectedValue);
        if (ddlSubClass.Items.Count == 1)
        {
            ddlSubClass.Enabled = false;
        }
        else
        {
            ddlSubClass.Enabled = true;
        }
        strSQL = "SELECT * FROM ACC_TAXMAINCLASS WHERE MAINCLASSID='" + ddlMainClass.SelectedValue + "'";
        DataTable DTThresHold = DU.getdata(strSQL);
        if (DTThresHold.Rows.Count > 0)
        {
            string strBillThreshold = DTThresHold.Rows[0]["DEFTHRESHOLD"].ToString();
            string strPerYearThreshold = DTThresHold.Rows[0]["DEFTHRESHOLDFORYEAR"].ToString();
            if (strBillThreshold == string.Empty)
            {
                strBillThreshold = "0";
            }
            if (strPerYearThreshold == string.Empty)
            {
                strPerYearThreshold = "0";
            }
            txtTDSPerBill.Text=string.Format("{0:0.00}",Convert.ToDouble(strBillThreshold));
            txtPerAnum.Text = string.Format("{0:0.00}", Convert.ToDouble(strPerYearThreshold));
        }
        hidTaxDetId.Value = "0";
        hidTaxMasterId.Value = "0";
        _BindAdditionalTaxDetails();
    }
    protected void ddlSubClass_SelectedIndexChanged(object sender, EventArgs e)
    {
        _GetDefaultTaxRate(ddlMainClass.SelectedValue, ddlSubClass.SelectedValue);
        _BindAllTaxDetails(ddlTaxType.SelectedValue, ddlMainClass.SelectedValue,ddlSubClass.SelectedValue);
        hidTaxDetId.Value = "0";
        hidTaxMasterId.Value = "0";
        _BindAdditionalTaxDetails();
    }
    protected void grdAdditionalTax_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlAddTaxLedger = (DropDownList)e.Row.FindControl("ddlAddTaxLedger");
            string strTaxMasterId = grdAdditionalTax.DataKeys[e.Row.RowIndex].Value.ToString();
            _BindAdditionalTaxLedger(strTaxMasterId, ddlAddTaxLedger);
        }
    }    
    protected void btnSave_Click(object sender, EventArgs e)  
    {
        try
        {
            using (TransactionScope scope = new TransactionScope())
            {
                _SaveTaxDetails(hidTaxDetId.Value, hidTaxMasterId.Value);
                scope.Complete();
                lblMsg.Text = "Updated Successfully..";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = ex.Message;
        }
    }
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
    protected void grdTaxDetails_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        string strTxDetId = grdTaxDetails.SelectedDataKey["TxDetId"].ToString();
        string strTaxMasterId = grdTaxDetails.SelectedDataKey["TaxMasterId"].ToString();
        Response.Redirect("TaxMaster.aspx?status=Edit&TxDetId=" + strTxDetId + "&TaxMasterId=" + strTaxMasterId);
    }    
}